package org.jeecg.modules.lab.chartAnaly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.lab.chartAnaly.mapper.ChartAnalyMapper;
import org.jeecg.modules.lab.chartAnaly.service.ChartAnalyService;
import org.jeecg.modules.lab.chartAnaly.vo.SbDeviceUseVo;
import org.jeecg.modules.lab.chartAnaly.vo.SbDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class ChartAnalyServiceImpl extends ServiceImpl<ChartAnalyMapper, SbDeviceVo> implements ChartAnalyService {

    @Autowired
    private ChartAnalyMapper chartAnalyMapper;

    @Override
    public List<SbDeviceVo> getDevUseAnalyList(String year,String devNumber, String devName, String status) throws ParseException {

        List<SbDeviceVo> sbDeviceVoList = chartAnalyMapper.getDevList(devNumber,devName,status);

        List<SbDeviceUseVo> sbDeviceUseVoList = chartAnalyMapper.getDeviceUseList(year,devNumber,devName,status);

        List<SbDeviceUseVo> sbDeviceAllUseList = chartAnalyMapper.getDeviceAllUseList(devNumber,devName,status);
        DecimalFormat df = new DecimalFormat("#0.00%");

        int num = DateUtils.getYearSum(year) + 1;

        for (SbDeviceVo dev : sbDeviceVoList) {
            String devId = dev.getId();
            Date startTime = null;
            Date endTime = null;
            long useDate = 0;

            for (int i = 0; i < sbDeviceUseVoList.size(); i++) {
                SbDeviceUseVo vo = sbDeviceUseVoList.get(i);
                String dId = vo.getId();
                if (devId.equals(dId)) {
                    Date aDate = vo.getStartTime();
                    Date bDate = vo.getEndTime();
                    if (startTime == null && endTime == null) {
                        startTime = aDate;
                        endTime = bDate;
                        if (dev.getStartTime() != null) {
                            dev.setStartTime(startTime);
                        }
                    } else {
                        int resultA = endTime.compareTo(aDate);
                        int resultB = endTime.compareTo(bDate);
                        if (resultA >= 0 && resultB >= 0) {
                            long n = countDate(startTime, endTime);
                            useDate = useDate + n + 1;
                            dev.setUseDate(String.valueOf(useDate));
                            startTime = null;
                            endTime = null;
                        } else if (resultA >= 0 && resultB <= 0) {
                            endTime = bDate;
                            continue;
                        } else {
                            long n = countDate(startTime, endTime);
                            useDate = useDate + n + 1;
                            dev.setUseDate(String.valueOf(useDate));
                            startTime = aDate;
                            endTime = bDate;
                        }
                        if ((i + 1) == sbDeviceUseVoList.size()) {
                            if (startTime != null && endTime != null) {
                                long n = countDate(startTime, endTime);
                                useDate = useDate + n + 1;
                                dev.setUseDate(String.valueOf(useDate));
                                double rate = Double.valueOf(useDate) / Double.valueOf(num);
                                dev.setUseRate(df.format(rate));
                                if (dev.getEndTime() != null) {
                                    dev.setEndTime(endTime);
                                }
                            }
                        }
                    }
                } else {
                    if (startTime != null && endTime != null) {
                        long n = countDate(startTime, endTime);
                        useDate = useDate + n + 1;
                        dev.setUseDate(String.valueOf(useDate));
                        double rate = Double.valueOf(useDate) / Double.valueOf(num);
                        dev.setUseRate(df.format(rate));
                        if (dev.getEndTime() != null) {
                            dev.setEndTime(endTime);
                        }
                        startTime = null;
                        endTime = null;
                        useDate = 0;
                        break;
                    }
                }
            }


            String operationDate = dev.getOperationDate();
            Integer numA = null;
            if (operationDate != null && !"".equals(operationDate)) {
                numA = DateUtils.getYearAll(operationDate);
            }
            Date startTimeB = null;
            Date endTimeB = null;
            long useDateB = 0;
            int numB = sbDeviceAllUseList.size();
            for (int i = 0; i < numB; i++) {
                SbDeviceUseVo vo = sbDeviceAllUseList.get(i);
                String dId = vo.getId();
                if (devId.equals(dId)) {
                    Date aDate = vo.getStartTime();
                    Date bDate = vo.getEndTime();
                    if (startTimeB == null && endTimeB == null) {
                        startTimeB = aDate;
                        endTimeB = bDate;
                    } else {
                        int resultA = endTimeB.compareTo(aDate);
                        int resultB = endTimeB.compareTo(bDate);
                        if (resultA >= 0 && resultB >= 0) {
                            long n = countDate(startTimeB, endTimeB);
                            useDateB = useDateB + n + 1;
                            dev.setSumUseDate(String.valueOf(useDate));
                            startTimeB = null;
                            endTimeB = null;
                        } else if (resultA >= 0 && resultB <= 0) {
                            endTimeB = bDate;
                            continue;
                        } else {
                            long n = countDate(startTimeB, endTimeB);
                            useDateB = useDateB + n + 1;
                            dev.setSumUseDate(String.valueOf(useDateB));
                            startTimeB = aDate;
                            endTimeB = bDate;
                        }
                        if ((i + 1) == numB) {
                            if (startTimeB != null && endTimeB != null) {
                                long n = countDate(startTime, endTime);
                                useDateB = useDateB + n + 1;
                                dev.setSumUseDate(String.valueOf(useDateB));
                                if (numA != null) {
                                    double rate = Double.valueOf(useDateB) / Double.valueOf(numA);
                                    dev.setSumUseRate(df.format(rate));
                                }

                            }
                        }
                    }
                } else {
                    if (startTimeB != null && endTimeB != null) {
                        long n = countDate(startTimeB, endTimeB);
                        useDateB = useDateB + n + 1;
                        dev.setSumUseDate(String.valueOf(useDateB));
                        if (numA != null) {
                            double rate = Double.valueOf(useDateB) / Double.valueOf(numA);
                            dev.setSumUseRate(df.format(rate));
                        }
                        startTimeB = null;
                        endTimeB = null;
                        useDateB = 0;
                        break;
                    }
                }
            }


        }
        return sbDeviceVoList;
    }


    private long countDate(Date startTime, Date endTime) {
        long start = startTime.getTime();
        long end = endTime.getTime();
        long num = end - start;
        long n = num / 24 / 60 / 60 / 1000;
        return n;
    }

}
